/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.dp1.gui.vista;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JPanel;
import pe.edu.pucp.dp1.gui.controlador.GestorGUI;
//import pe.edu.pucp.dp1.lectura.modelo.ObjCar;
import Shared.ObjCar;

public class Mapa extends JComponent {

    public static GestorGUI gestorGUI = GestorGUI.getInstance();

    int PIX_TILE = 10;//Número de pixeles de largo de un tile

    BufferedImage buffMap;
    //Graphics grafMap;
    char[][] auxTer;
    char[][] auxInt;
    int[][] auxIntDur;
    /*ThreadMap threadMap;*/

    public Mapa() {
        //setPreferredSize(new Dimension(100, 100));

    }

    private Image tileC;
    private Image tileI;
    private Image tileJ;
    private Image tileN;
    private Image tileH;
    private Image tileV;
    private Image tileP;

    @Override
    public void paint(Graphics g) {
        super.paint(g); //To change body of generated methods, choose Tools | Templates.
        Graphics grafMap;
        try {
            buffMap = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/cuadro500x500.jpg"));//Puede usar fondo

        } catch (Exception e) {
            System.out.println("error");
        }
        grafMap = buffMap.getGraphics();

        genMap(grafMap);
        //refInter(grafMap);
        actInter(grafMap);
        actVehic(grafMap);
        g.drawImage(buffMap, 0, 0, null);//this        
    }

    private void genMap(Graphics g) {
        try {
            tileC = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/cru10x10.jpg"));
            tileI = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/hor10x10.jpg"));
            tileJ = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/ver10x10.jpg"));
            tileN = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/nad10x10.jpg"));
        } catch (Exception e) {
        }

        auxTer = gestorGUI.genMapPistas();

        for (int i = 0; i < auxTer.length; i++) {
            for (int j = 0; j < auxTer[i].length; j++) {
                if (auxTer[i][j] == 'C') {
                    g.drawImage(tileC, i * PIX_TILE, (auxTer[i].length - 1 - j) * PIX_TILE, this);
                } else if (auxTer[i][j] == 'I') {
                    g.drawImage(tileI, i * PIX_TILE, (auxTer[i].length - 1 - j) * PIX_TILE, this);
                } else if (auxTer[i][j] == 'J') {
                    g.drawImage(tileJ, i * PIX_TILE, (auxTer[i].length - 1 - j) * PIX_TILE, this);
                } else if (auxTer[i][j] == 'N') {
                    g.drawImage(tileN, i * PIX_TILE, (auxTer[i].length - 1 - j) * PIX_TILE, this);
                }

            }
        }
    }

    private void actInter(Graphics g) {
        try {
            tileC = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/cru10x10.jpg"));
            tileH = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/cruh10x10a.jpg"));
            tileV = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/cruv10x10a.jpg"));
        } catch (Exception e) {
        }

        auxInt = gestorGUI.getMapInter();
        auxIntDur = gestorGUI.getMapInterDur();

        for (int i = 0; i < auxInt.length; i++) {
            for (int j = 0; j < auxInt[i].length; j++) {
                if (auxInt[i][j] == 'H') {
                    g.drawImage(tileH, i * PIX_TILE, (auxInt[i].length - 1 - j) * PIX_TILE, this);
                    String aux = String.valueOf(auxIntDur[i][j]);
                    g.setColor(Color.BLACK);
                    g.setFont(new Font("default", Font.BOLD, 16));
                    g.drawString(aux, i * PIX_TILE + 13, (auxInt[i].length - 1 - j) * PIX_TILE - 3);//OJO +13 y -3 en X e Y son para el ajuste visual
                } else if (auxInt[i][j] == 'V') {
                    g.drawImage(tileV, i * PIX_TILE, (auxInt[i].length - 1 - j) * PIX_TILE, this);
                    String aux = String.valueOf(auxIntDur[i][j]);
                    g.setColor(Color.BLACK);
                    g.setFont(new Font("default", Font.BOLD, 16));
                    g.drawString(aux, i * PIX_TILE + 13, (auxInt[i].length - 1 - j) * PIX_TILE - 3);//OJO +13 y -3 en X e Y son para el ajuste visual
                } else if (auxInt[i][j] == 'C') {
                    g.drawImage(tileC, i * PIX_TILE, (auxInt[i].length - 1 - j) * PIX_TILE, this);
                    String aux = String.valueOf(" ");
                    g.setColor(Color.BLACK);
                    g.setFont(new Font("default", Font.BOLD, 16));
                    g.drawString(aux, i * PIX_TILE + 13, (auxInt[i].length - 1 - j) * PIX_TILE - 3);
                }
            }
        }
    }

    private void actVehic(Graphics g) {

        ArrayList<ObjCar> auxVeh;
        try {
            tileH = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/apoint4x4.jpg"));
            tileV = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/bpoint4x4.jpg"));
        } catch (Exception e) {
        }

        if (gestorGUI.getVehiculos() != null) {
            auxVeh = gestorGUI.getVehiculos();

            for (int i = 0; i < auxVeh.size(); i++) {
                //System.out.println("PosX: "+auxVeh.get(i).getPosX());
                //System.out.println("PosY: "+auxVeh.get(i).getPosY());
                //System.out.println("Sentido: "+auxVeh.get(i).getSentido());
                //HC 1000=10 cuadras, -1 porque empieza en 0
                // Se divide entre 2: 1 cuadra = 100, en pixeles = 50
                int posX = ((auxVeh.get(i).getPosX() % 1000) / 2);
                int posY = 500 - ((auxVeh.get(i).getPosY() % 1000) / 2);
                //posX=0;
                //posY=500-0;
                int offSetX = 0;//Posicion en la pista
                int offSetY = 0;//Posicion en la pista

                if ((auxVeh.get(i).getSentido() == 'O')) {
                    g.drawImage(tileH, posX + 4, posY - 4, this);
                } else if (auxVeh.get(i).getSentido() == 'E') {
                    g.drawImage(tileV, posX + 4, posY - 10, this);
                } else if (auxVeh.get(i).getSentido() == 'N') {
                    g.drawImage(tileH, posX + 6, posY - 4, this);
                } else if (auxVeh.get(i).getSentido() == 'S') {
                    g.drawImage(tileV, posX + 0, posY - 10, this);
                }
                else {
//                    System.out.println("Mapa: Vehiculo sin direccion");
//                    g.drawImage(tileH, posX + 4, posY - 4, this);
                }

            }
            //g.drawImage(tileP, 11, 2, this);
        } else {
            //System.out.println("Mapa: Vehiculos NULL");
        }
    }
    


//    private void refInter(Graphics g) {
//        try {
//            tileH = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/cruh10x10.jpg"));
//            tileV = ImageIO.read(getClass().getResource("/pe/edu/pucp/dp1/gui/imagenes/cruv10x10.jpg"));
//        } catch (Exception e) {
//        }
//
//        auxInt = gestorGUI.getMapInter();
//        auxIntDur = gestorGUI.getMapInterDur();
//
//        for (int i = 0; i < auxInt.length; i++) {
//            for (int j = 0; j < auxInt[i].length; j++) {
//
//                String aux = String.valueOf(" ");
//                g.setColor(Color.BLACK);
//                g.setFont(new Font("default", Font.BOLD, 16));
//                g.drawString(aux, i * PIX_TILE + 13, (auxInt[i].length - 1 - j) * PIX_TILE - 3);
//
//            }
//        }
//    }
}
